{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "s = pd.Series([1,3,5,6,8], index=list('abcde'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1.0\n",
       "b    3.0\n",
       "c    5.0\n",
       "d    6.0\n",
       "e    8.0\n",
       "f    NaN\n",
       "g    NaN\n",
       "dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.reindex(list('abcdefg'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    3\n",
       "c    5\n",
       "d    6\n",
       "e    8\n",
       "f    8\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.reindex(list('abcdefg'), method='ffill')\n",
    "# 将数据填充为上一个数值,bfill向后填充，填充后面一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.071821</td>\n",
       "      <td>0.099286</td>\n",
       "      <td>0.535548</td>\n",
       "      <td>-3.036449</td>\n",
       "      <td>0.120340</td>\n",
       "      <td>-2.417862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.004446</td>\n",
       "      <td>1.470576</td>\n",
       "      <td>-2.293578</td>\n",
       "      <td>-0.466938</td>\n",
       "      <td>1.775602</td>\n",
       "      <td>0.210239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>1.101720</td>\n",
       "      <td>-0.583805</td>\n",
       "      <td>-0.714052</td>\n",
       "      <td>0.222069</td>\n",
       "      <td>-0.306590</td>\n",
       "      <td>0.988400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-1.248326</td>\n",
       "      <td>-0.996326</td>\n",
       "      <td>-0.779269</td>\n",
       "      <td>1.204242</td>\n",
       "      <td>0.790958</td>\n",
       "      <td>-0.180923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        one       two     three      four      five       six\n",
       "A  0.071821  0.099286  0.535548 -3.036449  0.120340 -2.417862\n",
       "B -0.004446  1.470576 -2.293578 -0.466938  1.775602  0.210239\n",
       "C  1.101720 -0.583805 -0.714052  0.222069 -0.306590  0.988400\n",
       "D -1.248326 -0.996326 -0.779269  1.204242  0.790958 -0.180923"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(4, 6), index=list('ABCD'), columns=['one', 'two', 'three', 'four', 'five', 'six'])# 二维数组的填充空白值\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.071821</td>\n",
       "      <td>0.099286</td>\n",
       "      <td>0.535548</td>\n",
       "      <td>-3.036449</td>\n",
       "      <td>0.120340</td>\n",
       "      <td>-2.417862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.004446</td>\n",
       "      <td>1.470576</td>\n",
       "      <td>-2.293578</td>\n",
       "      <td>-0.466938</td>\n",
       "      <td>1.775602</td>\n",
       "      <td>0.210239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>1.101720</td>\n",
       "      <td>-0.583805</td>\n",
       "      <td>-0.714052</td>\n",
       "      <td>0.222069</td>\n",
       "      <td>-0.306590</td>\n",
       "      <td>0.988400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-1.248326</td>\n",
       "      <td>-0.996326</td>\n",
       "      <td>-0.779269</td>\n",
       "      <td>1.204242</td>\n",
       "      <td>0.790958</td>\n",
       "      <td>-0.180923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        one       two     three      four      five       six\n",
       "A  0.071821  0.099286  0.535548 -3.036449  0.120340 -2.417862\n",
       "B -0.004446  1.470576 -2.293578 -0.466938  1.775602  0.210239\n",
       "C  1.101720 -0.583805 -0.714052  0.222069 -0.306590  0.988400\n",
       "D -1.248326 -0.996326 -0.779269  1.204242  0.790958 -0.180923\n",
       "E       NaN       NaN       NaN       NaN       NaN       NaN\n",
       "F       NaN       NaN       NaN       NaN       NaN       NaN"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.reindex(index=list('ABCDEF'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.071821</td>\n",
       "      <td>0.099286</td>\n",
       "      <td>0.535548</td>\n",
       "      <td>-3.036449</td>\n",
       "      <td>0.120340</td>\n",
       "      <td>-2.417862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.004446</td>\n",
       "      <td>1.470576</td>\n",
       "      <td>-2.293578</td>\n",
       "      <td>-0.466938</td>\n",
       "      <td>1.775602</td>\n",
       "      <td>0.210239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>1.101720</td>\n",
       "      <td>-0.583805</td>\n",
       "      <td>-0.714052</td>\n",
       "      <td>0.222069</td>\n",
       "      <td>-0.306590</td>\n",
       "      <td>0.988400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-1.248326</td>\n",
       "      <td>-0.996326</td>\n",
       "      <td>-0.779269</td>\n",
       "      <td>1.204242</td>\n",
       "      <td>0.790958</td>\n",
       "      <td>-0.180923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        one       two     three      four      five       six\n",
       "A  0.071821  0.099286  0.535548 -3.036449  0.120340 -2.417862\n",
       "B -0.004446  1.470576 -2.293578 -0.466938  1.775602  0.210239\n",
       "C  1.101720 -0.583805 -0.714052  0.222069 -0.306590  0.988400\n",
       "D -1.248326 -0.996326 -0.779269  1.204242  0.790958 -0.180923\n",
       "E  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000\n",
       "F  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.reindex(index=list('ABCDEF'), fill_value=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>100.000000</td>\n",
       "      <td>0.099286</td>\n",
       "      <td>0.535548</td>\n",
       "      <td>-3.036449</td>\n",
       "      <td>0.120340</td>\n",
       "      <td>-2.417862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.004446</td>\n",
       "      <td>1.470576</td>\n",
       "      <td>-2.293578</td>\n",
       "      <td>-0.466938</td>\n",
       "      <td>1.775602</td>\n",
       "      <td>0.210239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>1.101720</td>\n",
       "      <td>-0.583805</td>\n",
       "      <td>-0.714052</td>\n",
       "      <td>0.222069</td>\n",
       "      <td>-0.306590</td>\n",
       "      <td>0.988400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-1.248326</td>\n",
       "      <td>-0.996326</td>\n",
       "      <td>-0.779269</td>\n",
       "      <td>1.204242</td>\n",
       "      <td>0.790958</td>\n",
       "      <td>-0.180923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          one       two     three      four      five       six\n",
       "A  100.000000  0.099286  0.535548 -3.036449  0.120340 -2.417862\n",
       "B   -0.004446  1.470576 -2.293578 -0.466938  1.775602  0.210239\n",
       "C    1.101720 -0.583805 -0.714052  0.222069 -0.306590  0.988400\n",
       "D   -1.248326 -0.996326 -0.779269  1.204242  0.790958 -0.180923"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['A']['one'] = 100\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>100.000000</td>\n",
       "      <td>0.099286</td>\n",
       "      <td>0.535548</td>\n",
       "      <td>-3.036449</td>\n",
       "      <td>0.120340</td>\n",
       "      <td>-2.417862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.004446</td>\n",
       "      <td>1.470576</td>\n",
       "      <td>-2.293578</td>\n",
       "      <td>-0.466938</td>\n",
       "      <td>1.775602</td>\n",
       "      <td>0.210239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>1.101720</td>\n",
       "      <td>-0.583805</td>\n",
       "      <td>-0.714052</td>\n",
       "      <td>0.222069</td>\n",
       "      <td>-0.306590</td>\n",
       "      <td>0.988400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-1.248326</td>\n",
       "      <td>-0.996326</td>\n",
       "      <td>-0.779269</td>\n",
       "      <td>1.204242</td>\n",
       "      <td>0.790958</td>\n",
       "      <td>-0.180923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          one       two     three      four      five       six\n",
       "A  100.000000  0.099286  0.535548 -3.036449  0.120340 -2.417862\n",
       "B   -0.004446  1.470576 -2.293578 -0.466938  1.775602  0.210239\n",
       "C    1.101720 -0.583805 -0.714052  0.222069 -0.306590  0.988400\n",
       "D   -1.248326 -0.996326 -0.779269  1.204242  0.790958 -0.180923"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.004446</td>\n",
       "      <td>1.470576</td>\n",
       "      <td>-2.293578</td>\n",
       "      <td>-0.466938</td>\n",
       "      <td>1.775602</td>\n",
       "      <td>0.210239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>1.101720</td>\n",
       "      <td>-0.583805</td>\n",
       "      <td>-0.714052</td>\n",
       "      <td>0.222069</td>\n",
       "      <td>-0.306590</td>\n",
       "      <td>0.988400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-1.248326</td>\n",
       "      <td>-0.996326</td>\n",
       "      <td>-0.779269</td>\n",
       "      <td>1.204242</td>\n",
       "      <td>0.790958</td>\n",
       "      <td>-0.180923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        one       two     three      four      five       six\n",
       "B -0.004446  1.470576 -2.293578 -0.466938  1.775602  0.210239\n",
       "C  1.101720 -0.583805 -0.714052  0.222069 -0.306590  0.988400\n",
       "D -1.248326 -0.996326 -0.779269  1.204242  0.790958 -0.180923"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop('A')# 丢弃行，二维数组本身没有变化，只是拷贝的一份数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.099286</td>\n",
       "      <td>0.535548</td>\n",
       "      <td>-3.036449</td>\n",
       "      <td>0.120340</td>\n",
       "      <td>-2.417862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>1.470576</td>\n",
       "      <td>-2.293578</td>\n",
       "      <td>-0.466938</td>\n",
       "      <td>1.775602</td>\n",
       "      <td>0.210239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-0.583805</td>\n",
       "      <td>-0.714052</td>\n",
       "      <td>0.222069</td>\n",
       "      <td>-0.306590</td>\n",
       "      <td>0.988400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-0.996326</td>\n",
       "      <td>-0.779269</td>\n",
       "      <td>1.204242</td>\n",
       "      <td>0.790958</td>\n",
       "      <td>-0.180923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        two     three      four      five       six\n",
       "A  0.099286  0.535548 -3.036449  0.120340 -2.417862\n",
       "B  1.470576 -2.293578 -0.466938  1.775602  0.210239\n",
       "C -0.583805 -0.714052  0.222069 -0.306590  0.988400\n",
       "D -0.996326 -0.779269  1.204242  0.790958 -0.180923"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop(['one'], axis=1)# 丢弃列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "one      101.248326\n",
       "two        2.466901\n",
       "three      2.829126\n",
       "four       4.240691\n",
       "five       2.082192\n",
       "six        3.406261\n",
       "dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对二维数组中的值做函数处理,默认按列处理\n",
    "df.apply(lambda x: x.max() - x.min())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    103.036449\n",
       "B      4.069181\n",
       "C      1.815772\n",
       "D      2.452568\n",
       "dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对二维数组中的值做函数处理,按行处理\n",
    "df.apply(lambda x: x.max() - x.min(), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df.apply?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-1.248326</td>\n",
       "      <td>-0.996326</td>\n",
       "      <td>-2.293578</td>\n",
       "      <td>-3.036449</td>\n",
       "      <td>-0.306590</td>\n",
       "      <td>-2.417862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>100.000000</td>\n",
       "      <td>1.470576</td>\n",
       "      <td>0.535548</td>\n",
       "      <td>1.204242</td>\n",
       "      <td>1.775602</td>\n",
       "      <td>0.988400</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            one       two     three      four      five       six\n",
       "min   -1.248326 -0.996326 -2.293578 -3.036449 -0.306590 -2.417862\n",
       "max  100.000000  1.470576  0.535548  1.204242  1.775602  0.988400"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def min_max(x):\n",
    "    return pd.Series([x.min(), x.max()], index=['min', 'max'])\n",
    "df.apply(min_max)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>-3.036449</td>\n",
       "      <td>100.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-2.293578</td>\n",
       "      <td>1.775602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-0.714052</td>\n",
       "      <td>1.101720</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-1.248326</td>\n",
       "      <td>1.204242</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        min         max\n",
       "A -3.036449  100.000000\n",
       "B -2.293578    1.775602\n",
       "C -0.714052    1.101720\n",
       "D -1.248326    1.204242"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.apply(min_max, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>100.000000</td>\n",
       "      <td>0.099286</td>\n",
       "      <td>0.535548</td>\n",
       "      <td>-3.036449</td>\n",
       "      <td>0.120340</td>\n",
       "      <td>-2.417862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.004446</td>\n",
       "      <td>1.470576</td>\n",
       "      <td>-2.293578</td>\n",
       "      <td>-0.466938</td>\n",
       "      <td>1.775602</td>\n",
       "      <td>0.210239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>1.101720</td>\n",
       "      <td>-0.583805</td>\n",
       "      <td>-0.714052</td>\n",
       "      <td>0.222069</td>\n",
       "      <td>-0.306590</td>\n",
       "      <td>0.988400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-1.248326</td>\n",
       "      <td>-0.996326</td>\n",
       "      <td>-0.779269</td>\n",
       "      <td>1.204242</td>\n",
       "      <td>0.790958</td>\n",
       "      <td>-0.180923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          one       two     three      four      five       six\n",
       "A  100.000000  0.099286  0.535548 -3.036449  0.120340 -2.417862\n",
       "B   -0.004446  1.470576 -2.293578 -0.466938  1.775602  0.210239\n",
       "C    1.101720 -0.583805 -0.714052  0.222069 -0.306590  0.988400\n",
       "D   -1.248326 -0.996326 -0.779269  1.204242  0.790958 -0.180923"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>100.000</td>\n",
       "      <td>0.099</td>\n",
       "      <td>0.536</td>\n",
       "      <td>-3.036</td>\n",
       "      <td>0.120</td>\n",
       "      <td>-2.418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.004</td>\n",
       "      <td>1.471</td>\n",
       "      <td>-2.294</td>\n",
       "      <td>-0.467</td>\n",
       "      <td>1.776</td>\n",
       "      <td>0.210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>1.102</td>\n",
       "      <td>-0.584</td>\n",
       "      <td>-0.714</td>\n",
       "      <td>0.222</td>\n",
       "      <td>-0.307</td>\n",
       "      <td>0.988</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-1.248</td>\n",
       "      <td>-0.996</td>\n",
       "      <td>-0.779</td>\n",
       "      <td>1.204</td>\n",
       "      <td>0.791</td>\n",
       "      <td>-0.181</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       one     two   three    four    five     six\n",
       "A  100.000   0.099   0.536  -3.036   0.120  -2.418\n",
       "B   -0.004   1.471  -2.294  -0.467   1.776   0.210\n",
       "C    1.102  -0.584  -0.714   0.222  -0.307   0.988\n",
       "D   -1.248  -0.996  -0.779   1.204   0.791  -0.181"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#formater = lambda x: '%.03f' % x # 保留3位小数\n",
    "formater = '{0:.03f}'.format\n",
    "df.applymap(formater)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-1.248326</td>\n",
       "      <td>-0.996326</td>\n",
       "      <td>-0.779269</td>\n",
       "      <td>1.204242</td>\n",
       "      <td>0.790958</td>\n",
       "      <td>-0.180923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.004446</td>\n",
       "      <td>1.470576</td>\n",
       "      <td>-2.293578</td>\n",
       "      <td>-0.466938</td>\n",
       "      <td>1.775602</td>\n",
       "      <td>0.210239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>1.101720</td>\n",
       "      <td>-0.583805</td>\n",
       "      <td>-0.714052</td>\n",
       "      <td>0.222069</td>\n",
       "      <td>-0.306590</td>\n",
       "      <td>0.988400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>100.000000</td>\n",
       "      <td>0.099286</td>\n",
       "      <td>0.535548</td>\n",
       "      <td>-3.036449</td>\n",
       "      <td>0.120340</td>\n",
       "      <td>-2.417862</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          one       two     three      four      five       six\n",
       "D   -1.248326 -0.996326 -0.779269  1.204242  0.790958 -0.180923\n",
       "B   -0.004446  1.470576 -2.293578 -0.466938  1.775602  0.210239\n",
       "C    1.101720 -0.583805 -0.714052  0.222069 -0.306590  0.988400\n",
       "A  100.000000  0.099286  0.535548 -3.036449  0.120340 -2.417862"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(by='one')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>100.000000</td>\n",
       "      <td>0.099286</td>\n",
       "      <td>0.535548</td>\n",
       "      <td>-3.036449</td>\n",
       "      <td>0.120340</td>\n",
       "      <td>-2.417862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>1.101720</td>\n",
       "      <td>-0.583805</td>\n",
       "      <td>-0.714052</td>\n",
       "      <td>0.222069</td>\n",
       "      <td>-0.306590</td>\n",
       "      <td>0.988400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.004446</td>\n",
       "      <td>1.470576</td>\n",
       "      <td>-2.293578</td>\n",
       "      <td>-0.466938</td>\n",
       "      <td>1.775602</td>\n",
       "      <td>0.210239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-1.248326</td>\n",
       "      <td>-0.996326</td>\n",
       "      <td>-0.779269</td>\n",
       "      <td>1.204242</td>\n",
       "      <td>0.790958</td>\n",
       "      <td>-0.180923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          one       two     three      four      five       six\n",
       "A  100.000000  0.099286  0.535548 -3.036449  0.120340 -2.417862\n",
       "C    1.101720 -0.583805 -0.714052  0.222069 -0.306590  0.988400\n",
       "B   -0.004446  1.470576 -2.293578 -0.466938  1.775602  0.210239\n",
       "D   -1.248326 -0.996326 -0.779269  1.204242  0.790958 -0.180923"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(by='one', ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "      <th>five</th>\n",
       "      <th>six</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   one  two  three  four  five  six\n",
       "A  4.0  3.0    4.0   1.0   2.0  1.0\n",
       "B  2.0  4.0    1.0   2.0   4.0  3.0\n",
       "C  3.0  2.0    3.0   3.0   1.0  4.0\n",
       "D  1.0  1.0    2.0   4.0   3.0  2.0"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.rank(method='first')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     a\n",
       "1     b\n",
       "2     b\n",
       "3     b\n",
       "4     b\n",
       "5     c\n",
       "6     c\n",
       "7     c\n",
       "8     d\n",
       "9     d\n",
       "10    d\n",
       "11    d\n",
       "dtype: object"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(list('abbbbcccdddd'))\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "d    4\n",
       "b    4\n",
       "c    3\n",
       "a    1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.value_counts()# 获取值的出现个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['a', 'b', 'c', 'd'], dtype=object)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.unique()#唯一的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
